<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: class.cacheFile.php 318 2013-09-26 19:49:13Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 318 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-26 19:49:13 +0000 (Thu, 26 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

if(DEVELOPER_MODUS == 'true') {
	require(DIR_FS_INC.'inc.delete_cache.php');
	deleteCacheFolders();
}

class chacheFile {
	public $type;
	public $source = array();
	public $name;
	public $data;
	var $css = array();
	var $js = array();
	
	function __construct($type) {
		$this->type = $type;
	}
	
	public function setCSS($path) {
		$this->css[] = array('path' => $path);
	}
	
	public function setJS($path, $create_cache = 'true') {
		$this->js[] = array('path' => $path, 'file' => $create_cache);
	}
	
	public function outputCSS() {
		$css = '';
		$css_file = '';
		reset($this->css);
		foreach($this->css AS $css_path) {
			$content ='';
			if(ADMINMODE_CACHE_JS=='false' || !file_exists($this->getCachePath($css_path['path']))) {
				if(file_exists($css_path['path'])) {
					$content = $this->getCleanCSS(file_get_contents($css_path['path']));
					file_put_contents($this->getCachePath($css_path['path']), $content);
					$css .= $content;
				}
			} else
				$css .= file_get_contents($this->getCachePath($css_path['path']));
		}
		
		$css_file = '_css_'.$this->getTempName($this->css);
		
		if(!file_exists(DIR_FS_CATALOG.'cache/'.$css_file)) {
			@unlink($css_file);
			file_put_contents(DIR_FS_CATALOG.'cache/'.$css_file, $css);
		}
		return file_get_contents(DIR_FS_CATALOG.'cache/'.$css_file);
	}
	
	public function outputJS() {
		global $db, $price;
		$js = '';
		$js_file = '';
		foreach($this->js AS $js_path) {
			$content='';
			$js .= "\n/*load ".$js_path['path']."*/\n";
			if($js_path['file'] == 'true') {
				if(ADMINMODE_CACHE_JS=='false' || !file_exists($this->getCachePath($js_path['path'])) && $js_path['path'] != 'cache/_cache_js_xajax.js') {
					if(file_exists($js_path['path'])) {
						$content = $this->getCleanJS(file_get_contents($js_path['path']));
						file_put_contents($this->getCachePath($js_path['path']), $content);
						$js .= $content;
					}
				} elseif($js_path['path'] == 'cache/_cache_js_xajax.js')
					$js .= file_get_contents($js_path['path']);
				else
					$js .= file_get_contents($this->getCachePath($js_path['path']));

			} elseif($js_path['file'] == 'false') {
				include($js_path['path']);
				$js .= $this->getCleanJS($content);
			}	
		}
		
		$js_file = '_js_'.$this->getTempName($this->js);
		return $js;
	}
	
	public function getCachePath($path) {
		$real = pathinfo($path);
		if($this->type == 'css')
			return DIR_FS_CATALOG.'cache/_cache_css_'.$real['basename'];
		elseif($this->type == 'js')
			return DIR_FS_CATALOG.'cache/_cache_js_'.$real['basename'];
	}
	
	public function getCleanCSS($data) {
		$raw = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $data);
		$raw = preg_replace('/\s\s+/', ' ', $raw);
		
		$spaces = array('{ ', ' }', ' {', '} ', ' ,', ', ', ': ', ' :', '; ', ' ;');
		
		foreach($spaces AS $space)
			$raw = str_replace($space, trim($space), $raw);
		
		$content = str_replace(array("\r\n", "\r", "\n", "\t"), '', $raw);
		
		return $content;
	}
	
	public function getCleanJS($data) {
		return $data = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $data);
	}
	
	public function getTempName($path) {
		foreach($this->{$this->type} AS $name_path)
			$name .= $this->getCachePath($name_path['path']);
		return md5($name);
	}
}